package com.kuanter.kuanterauto.main;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.activity.MyMessageActivity;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.dataservices.PushServices;
import com.kuanter.kuanterauto.db.DBManager;
import com.kuanter.kuanterauto.entity.MessageInfoList;
import com.kuanter.kuanterauto.net.HttpUtil;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

/** 
 * @author utopia
 * @version 2013-9-7 ����3:43:41
 * 
 */
public class UpdateReceive extends BroadcastReceiver {
	DBManager dbManager;
	NotificationManager notificationManager=null;
	 Notification notification=null;

	@Override
	public void onReceive(Context context, Intent intent) {
		dbManager = new DBManager(context);
		if(intent.getAction().equals("com.update.Ad")) {
			notificationManager=(NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
			// 定义Notification的各种属性   
	         notification =new Notification(R.drawable.ic_launcher, "宽途汽车", System.currentTimeMillis()); 
	        notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
	        notification.flags |= Notification.FLAG_AUTO_CANCEL;
			
			boolean push = SharePrefenceUtil.getMessagePush(context);
			if (push) {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("deviceToken",
						MainActivity.Token));
				/*params.add(new BasicNameValuePair("notAlert",
						"1"));*/
				String result = HttpUtil.requestStringForPost1(
						MainConstants.HOST_test + "query_unRead_message", params);
				try {
					if (StringUtils.isSuccess(result)) {
						if (PushServices.messageInfos != null) {
							PushServices.messageInfos.clear();
							PushServices.messageInfos = null;
						}
						MessageInfoList msgList = new MessageInfoList(result);
						PushServices.messageInfos = msgList.getMessageInfos();
						//TODO
						/*MessageInfo info = new MessageInfo();
						info.setContent("GNU是自由软件之父 Richard Stallman在1984年组织开发的一个完全基于自由软件的软件体系，与此相应的有一分通用公共许可证(General Public License，简称GPL)。Linux以及与她有关的大量软件是在GPL的推动下开发和发布的自由软件之父 Stallman像一个神态庄严的传教士一样喋喋不休地到处传播自由软件的福音，阐述他创立GNU的梦想：“自由的思想，而不是免费的午餐”。这位自由软件的“顶级神甫”为自己的梦想付出了大半生的努力，他不但自己创作了许多自由软件如GCC和GDB");
						info.setPushTime(new Date().toLocaleString());
						info.setTitle("GNU是自由软件");
						messageInfos = new ArrayList<MessageInfo>();
						messageInfos.add(info);*/
						//TODO
						
						if (PushServices.messageInfos != null) {
							PushServices.unReadMessage = PushServices.unReadMessage + PushServices.messageInfos.size();
							dbManager.add(PushServices.messageInfos);
							dbManager.closeDB();
							notification(context);
							Intent intent1 = new Intent(HomeActivityGroup.show_action);
							intent1.putExtra("status", HomeActivityGroup.show);
							context.sendBroadcast(intent1);
							
							Intent set_intent = new Intent("com.setting.show");
							context.sendBroadcast(set_intent);
							
							Log.i("aaa", "发送消息");
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	private void notification(Context context){
		CharSequence contentTitle ="消息"; // 通知栏标题   
        CharSequence contentText ="您有" + PushServices.unReadMessage + "条未读新的消息"; // 通知栏内容   
        
        Intent notificationIntent =new Intent(context.getApplicationContext(), MyMessageActivity.class); // 点击该通知后要跳转的Activity
//        notificationIntent.setAction(MainConstants.PUSH_ACTION);
        
        SharedPreferences sp = context.getSharedPreferences("push", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putBoolean("push", true);
		editor.commit();
//        notificationIntent.putExtra("PUSH", true);
        PendingIntent contentItent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT );   //
        notification.setLatestEventInfo(context, contentTitle, contentText, contentItent);   
        // 把Notification传递给NotificationManager   
        notificationManager.notify(0, notification); 
        
        System.out.println(StringUtils.getTime(System.currentTimeMillis()));
	}
}
